import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import * as path from "path";
import viteCompression from "vite-plugin-compression";
import UnoCss from "unocss/vite";

export default defineConfig(({ command }) => {
  const isDev = command === "serve";

  return {
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "./src"),
      },
    },
    plugins: [
      react(),
      UnoCss(),
      viteCompression({
        algorithm: "gzip",
        ext: ".gz",
        threshold: 1025,
        deleteOriginFile: false,
      }),
    ],
    server: {
      proxy: {
        "/api": {
          target: "http://localhost:3005", // 后端接口地址
          changeOrigin: true, // 是否更改 origin 头
        },
      },
      host: "0.0.0.0",
      port: 3000,
      open: true,
      hmr: true,
    },
    build: {
      assetsDir: "static",
      sourcemap: false,
      cssCodeSplit: true,
      emptyOutDir: true,
      chunkSizeWarningLimit: 10000,
      terserOptions: {
        compress: {
          drop_console: true,
          drop_debugger: true,
        },
      },
      rollupOptions: {
        output: {
          chunkFileNames: "static/js/[name]-[hash].js",
          entryFileNames: "static/js/[name]-[hash].js",
          assetFileNames: "static/[ext]/[name]-[hash].[ext]",
        },
      },
    },
  };
});
